﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_ArchiveJKDal
    {
        public static List<V_ARCHIVEJK> Get_ArchiveJK(int status , string type, bool bNoCL)
        {
            SGArchiveSystem7xEntities conn = new SGArchiveSystem7xEntities();
            var query = conn.V_ARCHIVEJK.Select(c => c);
            if (status > -1)
                query = query.Where(c => c.JK_Status == status);
            if (type != null && type != "")
                query = query.Where(c => c.JK_Type == type);
            if (bNoCL)
                query = query.Where(c => c.JK_CLR == null || c.JK_CLR == "");
            return query.OrderByDescending(c => c.JK_Date).ToList();
        }


        /// <summary>
        /// 处理意见
        /// </summary>
        /// <param name="sIds"></param>
        /// <param name="clyj"></param>
        /// <returns></returns>
        public static int Set_CLJK(int[] sIds, string clyj, string clr)
        {
            var  dt =  DateTime.Now;
            SGArchiveSystem7xEntities conn = new SGArchiveSystem7xEntities();
            try
            {
                var query = conn.T_ArchiveJK.Select(c => c).Where(c => sIds.Contains(c.JK_ID));
                foreach(var n in query)
                {
                    ///获取到的N，判断是否最后一次的数据，如果是，那么把档案表里的不合规改为合规
                    n.JK_ArchiveType = clyj;
                    n.JK_CLR = clr;
                    n.JK_CLSJ = dt;

                    var query2 = conn.T_ArchiveAj.Select(c => c).Where(c => c.I01 == n.JK_ID);
                    foreach (var nn in query2)
                        nn.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_非授权已处理;
                    //conn.T_ArchiveAj.Select(c=>c).Where(c=>c.ID)
                }
                conn.SaveChanges();

                return 1;
            }
            catch { return -1; }
        }

        /// <summary>
        /// 所有没有授权的在库房外的档案
        /// </summary>
        /// <returns></returns>
        public static List<V_ArchiveAj> Get_ArchiveAJ(string ar_name, string dh, string tm, string nd,  out int count, int start, int row)
        {
            count = 0;
            SGArchiveSystem7xEntities conn = new SGArchiveSystem7xEntities();
            try
            {
                int kfzt = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_借出;
                int ajzt = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_授权;
                var query = conn.V_ArchiveAj.Select(c => c).Where(c => c.KFZT == kfzt && c.AJZT != ajzt); //.OrderBy(c => c.AR_NAME).ThenBy(c => c.DH).ToList();
                if (ar_name != null && ar_name != "")
                    query = query.Where(c => c.AR_NAME == ar_name);
                if (dh != null && dh != "")
                    query = query.Where(c => c.DH.Contains(dh));
                if (tm != null && tm != "")
                    query = query.Where(c => c.TM.Contains(tm));
                if(nd != null && nd != "")
                {
                    int ind = int.Parse(nd);
                    query = query.Where(c => c.ND == ind);
                }
               

                //query = query.OrderBy(c => c.AR_NAME).ThenBy(c => c.DH);
                count = query.Count();
                return query.OrderBy(c => c.ID).Skip(start).Take(row).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// 监控记录
        /// </summary>
        /// <param name="ar_name"></param>
        /// <param name="dh"></param>
        /// <param name="tm"></param>
        /// <param name="nd"></param>
        /// <param name="count"></param>
        /// <param name="start"></param>
        /// <param name="row"></param>
        /// <param name="type">类型: 1. 出库， 2. 入库</param>
        /// <returns></returns>
        public static List<V_ARCHIVEJK> Get_ArchiveJK(string ar_name, string dh, string tm, string nd, string  starttime, string endtime,string ftm, int type, out int count, int start, int row)
        {
            count = 0;
            SGArchiveSystem7xEntities conn = new SGArchiveSystem7xEntities();
            try
            {
                var query = conn.V_ARCHIVEJK.Select(c => c); //.OrderBy(c => c.AR_NAME).ThenBy(c => c.DH).ToList();
                if (ar_name != null && ar_name != "")
                    query = query.Where(c => c.AR_NAME == ar_name);
                if (dh != null && dh != "")
                    query = query.Where(c => c.JK_ArchiveDH.Contains(dh));
                if (tm != null && tm != "")
                    query = query.Where(c => c.JK_TM.Contains(tm));
                if (nd != null && nd != "")
                {
                    int ind = int.Parse(nd);
                    query = query.Where(c => c.JK_ND == ind);
                }

                if (ftm != null && ftm != "")
                    query = query.Where(c => c.JK_FTM.Contains(ftm));

                if (starttime != null && starttime != "")
                {
                    DateTime st = DateTime.Parse(starttime);
                    query = query.Where(c => c.JK_Date >= st);
                }
                if (endtime != null && endtime != "")
                {
                    DateTime et = DateTime.Parse(endtime);
                    query = query.Where(c => c.JK_Date <= et);
                }

                if(type == 1)
                {
                    query = query.Where(c => c.JK_Type.Contains("入库"));
                }
                else if(type == 2)
                {
                    query = query.Where(c => c.JK_Type.Contains("出库"));
                }

                //query = query.OrderByDescending(c=>c.JK_Date)
                count = query.Count();
                return query.OrderByDescending(c => c.JK_Date).Skip(start).Take(row).ToList();
            }
            catch { return null; }
        }


        /// <summary>
        /// 手动入库档案并建立监控记录
        /// </summary>
        /// <returns></returns>
        public static int Set_ArchiveAJRKAndAddJK(int[] Ids, string clr)
        {
            try
            {
                SGArchiveSystem7xEntities conn = new SGArchiveSystem7xEntities();
                var dt = DateTime.Now;
                foreach (var i in Ids)
                {
                    ///首先获取要入库的档案
                    var query = conn.T_ArchiveAj.Select(c => c).Where(c => c.ID == i);
                    foreach (var n in query)
                    {
                        n.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_在库;
                        n.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_授权;

                        ///添加一个记录
                        T_ArchiveJK en = new T_ArchiveJK();
                        en.JK_ArchiveDH = n.DH;
                        en.JK_ArchiveType = "非授权手动入库";
                        en.JK_AID = n.ID;
                        en.JK_ARID = n.ARID;
                        en.JK_CLR = clr;
                        en.JK_CLSJ = dt;
                        en.JK_Date = dt;
                        en.JK_ND = n.ND;
                        en.JK_Status = 1;
                        en.JK_Tag = n.DZWJ;
                        en.JK_TM = n.TM;
                        en.JK_Type = "入库";
                        conn.T_ArchiveJK.Add(en);
                    }
                    conn.SaveChanges();
                }
                return 1;
            }
            catch { return -1; }
        }
    }
}
